﻿CREATE PROCEDURE [dbo].[message_get_listing]
    @recepientId int,
    @senderId int,
    @page int = 0,
    @size int = 0
AS
    SELECT 
        Messages.Message,
        Users.Email,
        Users.Avatar,
        Users.Login
    FROM Messages,Users 
    WHERE
        (Users.UserId = Messages.RecipientId and Users.UserId = @recepientId)
        or(Users.UserId = Messages.RecipientId and Users.UserId = @senderId)
    ORDER BY Messages.DateOfSend DESC
            OFFSET((@page-1)*@size) ROWS
        FETCH NEXT @size ROWS ONLY;
        
DECLARE @TotalCount int
SELECT @TotalCount = Count(Messages.Message)
FROM Messages,Users
    WHERE
        (Users.UserId = Messages.RecipientId and Users.UserId = @recepientId)
        or(Users.UserId = Messages.SenderId and Users.UserId = @senderId)
RETURN @TotalCount
